package com.example.a2032.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;

import com.example.a2032.R;
import com.example.a2032.Utils.Utils;

import java.util.Calendar;

import butterknife.BindColor;

public class MyWatchView extends View {
    private Bitmap bgBitmap;
    Bitmap hourBitmap;
    Bitmap minBitmap;
    Bitmap secondBitmap;
    int watchWidth;
    int watchHeight;
    TextPaint mTextPaint;
    int dateTextSize = 28;
    int weekDaysSize = 26;
    float circleRadius=12.5f;
    String[] weekArray;
    String showDate;
    String showWeekDays;
    float scaleHeightValue;
    float scaleWidthValue;
    //屏幕宽高直接写死
    float screenWidth = 320f;
    float screenHeight = 320f;
    float centerX = 0;
    float centerY = 0;
    Typeface timeTypeface;
    Paint indicatorPaint;

    int watchTextColor;
    public MyWatchView(Context context) {
        super(context);
    }

    public MyWatchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private static final String TAG = "MyWatchView";

    private void init(Context context) {
        watchTextColor=getResources().getColor(R.color.watch_textColor);
        bgBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.watch_bg);
        hourBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.watch_hour);
        minBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.watch_minute);
        secondBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.watch_second);
        watchHeight = bgBitmap.getHeight();
        watchWidth = bgBitmap.getWidth();
        scaleHeightValue = screenHeight / watchHeight;
        scaleWidthValue = screenWidth / watchWidth;
        mTextPaint = new TextPaint();
        mTextPaint.setAntiAlias(true);


        mTextPaint.setColor(watchTextColor);
        mTextPaint.setStrokeWidth(2);
        indicatorPaint = new Paint();
        indicatorPaint.setAntiAlias(true);
        timeTypeface = ResourcesCompat.getFont(context, R.font.din_bold);

        weekArray = context.getResources().getStringArray(R.array.week_array);

        centerX = screenWidth / 2;
        centerY = screenHeight / 2;
        initTimeString(Calendar.getInstance());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBackground(canvas);
        drawWeek(canvas);
        drawDate(canvas);
        drawTimeIndicator(canvas);
        drawIndicatorCircle(canvas);
    }

    private void drawBackground(Canvas canvas) {
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidthValue, scaleHeightValue);
        canvas.drawBitmap(bgBitmap, matrix, null);
    }

    private void drawTimeIndicator(Canvas canvas) {
        drawHour(canvas, hourDegrees);
        drawMinutes(canvas, minDegrees);
        drawSecond(canvas,  secondDegrees);
    }

    private void drawIndicatorCircle(Canvas canvas) {
        indicatorPaint.setColor(Color.parseColor("#ff5408"));
        canvas.drawCircle(centerX,centerY,circleRadius+3,indicatorPaint);
        indicatorPaint.setColor(Color.WHITE);
        canvas.drawCircle(centerX,centerY,circleRadius,indicatorPaint);
        indicatorPaint.setColor(Color.BLACK);
        canvas.drawCircle(centerX,centerY,circleRadius-3,indicatorPaint);
        indicatorPaint.setColor(Color.parseColor("#ff5408"));
        canvas.drawCircle(centerX,centerY,circleRadius-6,indicatorPaint);
    }


    void drawHour(Canvas canvas, float rotate) {
        indicatorPaint.setColor(Color.WHITE);
        indicatorPaint.setStrokeWidth(11);
        indicatorPaint.setStrokeCap(Paint.Cap.ROUND);
        canvas.save();
        canvas.rotate(rotate, centerX, centerY);
        canvas.drawLine(centerX, centerY-circleRadius, centerX, centerY - 78, indicatorPaint);
        canvas.restore();
    }
    void drawMinutes(Canvas canvas, float rotate) {
        indicatorPaint.setColor(Color.WHITE);
        indicatorPaint.setStrokeWidth(11);
        indicatorPaint.setStrokeCap(Paint.Cap.ROUND);
        canvas.save();
        canvas.rotate(rotate, centerX, centerY);
        canvas.drawLine(centerX, centerY-circleRadius, centerX, centerY - 106, indicatorPaint);
        canvas.restore();
    }
    void drawSecond(Canvas canvas, float rotate) {
        indicatorPaint.setColor(Color.parseColor("#ff0000"));
        indicatorPaint.setStrokeWidth(3);
        indicatorPaint.setStrokeCap(Paint.Cap.SQUARE);
        canvas.save();
        canvas.rotate(rotate, centerX, centerY);
        canvas.drawLine(centerX, centerY-circleRadius, centerX, centerY - 109, indicatorPaint);
        canvas.restore();
    }
    private void drawDate(Canvas canvas) {
        mTextPaint.setTextSize(dateTextSize);
        mTextPaint.setTypeface(timeTypeface);
        Rect rect = new Rect();
        mTextPaint.getTextBounds(showDate, 0, showDate.length(), rect);
        Paint.FontMetricsInt fm = mTextPaint.getFontMetricsInt();
        //圆直径25，字离圆心间隔28px
        float startX = centerX - rect.width() - 40.5f;
        float startY = centerY - fm.descent + (fm.bottom - fm.top) / 2 - 2;
        canvas.drawText(showDate, startX, startY, mTextPaint);
    }

    private void drawWeekDays(Canvas canvas, float centerX) {
        mTextPaint.setTextSize(weekDaysSize);
        mTextPaint.setTypeface(null);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(watchTextColor);
        Rect rect = new Rect();
        mTextPaint.getTextBounds(showWeekDays, 0, showWeekDays.length(), rect);
        Paint.FontMetricsInt fm = mTextPaint.getFontMetricsInt();
        float startY = centerY - fm.descent + (fm.bottom - fm.top) / 2 - 2;
        canvas.drawText(showWeekDays, centerX - rect.width() / 2, startY, mTextPaint);


    }

    private void drawWeek(Canvas canvas) {
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setStyle(Paint.Style.STROKE);
        RectF rectF = new RectF();
        rectF.left = centerX + 28f;
        rectF.right = rectF.left + 86;
        rectF.top = centerY - 25;
        rectF.bottom = centerY + 25;
        canvas.drawRect(rectF, mTextPaint);
        float centerX = rectF.left + 41;
        drawWeekDays(canvas, centerX);
    }

    float hourDegrees;
    float minDegrees;
    float secondDegrees;

    private void initTimeString(Calendar calendar) {
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minutes = calendar.get(Calendar.MINUTE);
        int seconds = calendar.get(Calendar.SECOND);
        float minPercent=minutes / 60f;
        hourDegrees = hour / 12f * 360+minPercent*30;
        minDegrees =minPercent * 360;
        secondDegrees = seconds / 60f * 360;
        int weekDays = calendar.get(Calendar.DAY_OF_WEEK);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        showWeekDays = weekArray[weekDays - 1];
        showDate = Utils.getFormatDateString(month + 1, day, ".");
    }

    public void changeTime(Calendar calendar) {
        initTimeString(calendar);
        postInvalidate();
    }
}
